﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using ASER.DataAccess.BaseDataAccess;
using ASER.DataEntity.Security;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ASER.DataAccess.Security
{
    /// <summary>
    /// 数据访问类:SysMenuDataAccess
    /// 文  件  名:SysMenuDataAccess.cs
    /// 说      明:06、菜单表(SysMenu)数据访问对象
    /// </summary>
    public class SysmenuDataAccess : BaseDataAccess.BaseDataAccess<SysMenuEntity>
    {
        #region 私有字段

        private readonly string DATABASE = "Platform";

        #endregion

        #region 公共方法

        public List<SysMenuEntity> GetMenus(SysOperatorEntity user)
        {
            List<SysMenuEntity> Result = null;
            string sqlText = @"SELECT A.* FROM Sys_Menu A JOIN Sys_MenuPosition B ON A.MenuID=B.MenuID
                           JOIN Sys_OperatorPosition C ON B.PositionCode = C.PositonCode
                            WHERE C.UserCode=@UserCode AND LEFT(MenuSeq ,11)='s0001.e0001'";
            Database db = DatabaseFactory.CreateDatabase("Platform");
            using (DbCommand Command = db.GetSqlStringCommand(sqlText))
            {
                db.AddInParameter(Command, "@UserCode", DbType.String, user.UserCode);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }

        /// <summary>
        /// 根据用户编号获得菜单
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="parentId">父节点编号</param>
        /// <returns></returns>
        public List<SysMenuEntity> GetMenus(string userId, string parentId)
        {
            List<SysMenuEntity> Result = null;
            string sqlText = @"SELECT A.*,B.PositionCode,C.UserCode from Sys_Menu A 
                                   JOIN Sys_MenuPosition     B ON A.MenuID=B.MenuID
                                   JOIN Sys_OperatorPosition C ON B.PositionCode=C.PositonCode
                               WHERE ParentsID=@parentId AND UserCode=@userId
                               ORDER BY DisplayOrder";
            Database db = DatabaseFactory.CreateDatabase(DATABASE);
            using (DbCommand Command = db.GetSqlStringCommand(sqlText))
            {
                db.AddInParameter(Command, "@userId", DbType.String, userId);
                db.AddInParameter(Command, "@parentId", DbType.String, parentId);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }
        /// <summary>
        /// 根据用户编号获得菜单
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <returns></returns>
        public List<SysMenuEntity> GetMenusByUserId(string userId)
        {
            List<SysMenuEntity> Result = null;
            //var strb = new StringBuilder();
            //strb.Append("select * from Sys_Menu where MenuID in(");
            //strb.Append("select MenuID from  Sys_MenuPosition where PositionCode in(");
            //strb.Append("select PositonCode from Sys_OperatorPosition where UserCode='@userId'))");
            string sqlText = @"SELECT A.*,B.PositionCode,C.UserCode from Sys_Menu A 
                                   JOIN Sys_MenuPosition     B ON A.MenuID=B.MenuID
                                   JOIN Sys_OperatorPosition C ON B.PositionCode=C.PositonCode
                               WHERE UserCode=@userId 
                               ORDER BY DisplayOrder";
            Database db = DatabaseFactory.CreateDatabase(DATABASE);
            using (DbCommand Command = db.GetSqlStringCommand(sqlText))
            {
                db.AddInParameter(Command, "@userId", DbType.String, userId);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }
        /// <summary>
        /// 根据父节点获得所有子节点
        /// </summary>
        /// <param name="parentId">父节点编号</param>
        /// <returns></returns>
        public List<SysMenuEntity> GetMenus(string parentId)
        {
            List<SysMenuEntity> Result = null;
            string sqlText = @"SELECT * FROM  Sys_Menu where ParentsID=@ParentsID order by DisplayOrder";
            Database db = DatabaseFactory.CreateDatabase(DATABASE);
            using (DbCommand Command = db.GetSqlStringCommand(sqlText))
            {
                db.AddInParameter(Command, "@ParentsID", DbType.String, parentId);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }
        /// <summary>
        /// 获取同等级下的节点
        /// </summary>
        /// <param name="menuLevel">节点等级</param>
        /// <returns></returns>
        public List<SysMenuEntity> GetMenus(int menuLevel)
        {
            List<SysMenuEntity> Result = null;
            string sqlText = @"SELECT * FROM  Sys_Menu where MenuLevel=@MenuLevel  order by DisplayOrder";
            Database db = DatabaseFactory.CreateDatabase(DATABASE);
            using (DbCommand Command = db.GetSqlStringCommand(sqlText))
            {
                db.AddInParameter(Command, "@MenuLevel", DbType.String, menuLevel);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }

        /// <summary>
        ///获取06、菜单表(SysMenu)所有数据
        /// </summary>
        public List<SysMenuEntity> GetSysMenus()
        {
            List<SysMenuEntity> Result = null;
            string sqlText = @"SELECT * FROM  Sys_Menu";
            Database db = DatabaseFactory.CreateDatabase(DATABASE);
            using (DbCommand Command = db.GetSqlStringCommand(sqlText))
            {
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillDataListGeneric(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }

        /// <summary>
        /// 获取06、菜单表(SysMenu)数据
        /// </summary>
        /// <param name="MenuID">菜单编号</param> 
        /// <returns>06、菜单表(SysMenu)实体</returns>
        public SysMenuEntity GetSysMenu(string MenuID)
        {
            string SqlText = "SELECT * FROM  Sys_Menu WHERE MenuID=@MenuID ";
            SysMenuEntity Result = null;
            Database db = DatabaseFactory.CreateDatabase(DATABASE);
            using (DbCommand Command = db.GetSqlStringCommand(SqlText))
            {
                db.AddInParameter(Command, "@MenuID", DbType.String, MenuID);
                IDataReader Reader = db.ExecuteReader(Command);
                try
                {
                    Result = FillEntities(Reader);
                }
                finally
                {
                    Reader.Close();
                    Reader.Dispose();
                }
            }
            return Result;
        }          

        #endregion
    }
}
